package com.xiaoxi.codemodelserver.core.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xiaoxi.codemodelserver.core.ICode;
import com.xiaoxi.codemodelserver.domain.dto.BMSDictDTO;
import com.xiaoxi.codemodelserver.service.BmsDictService;
import com.xiaoxi.common.constant.enumeration.dict.TitleLevelEnums;
import com.xiaoxi.common.constant.enumeration.statuscode.system.CodeModelSystemEnums;
import com.xiaoxi.common.exception.BMSException;
import com.xiaoxi.domain.quick.BmsCodeModel;
import com.xiaoxi.domain.quick.BmsDict;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author xiaoxi
 * @ProjectName springcloudbms
 * @PackageName com.xiaoxi.codemodelserver.core.impl.DictCodeModelServiceImpl
 * @ClassName DictCodeModelServiceImpl
 * @Description TODO
 * @Date 2023/2/22 15:43
 * @Version 1.0
 */
@Service("dict")
@Slf4j
public class DictCodeModelServiceImpl implements ICode<BmsDict> {

    @Resource
    private BmsDictService bmsDictService;

    @Override
    public List<BmsDict> getCodeList(BmsCodeModel bmsCodeModel) throws BMSException {

        String referenceCode = bmsCodeModel.getReferenceCode();
        BMSDictDTO bmsDictDTO = new BMSDictDTO();

        bmsDictDTO.setDictCode(referenceCode);
        bmsDictDTO.setType(TitleLevelEnums.SECOND_TITLE.getCode());

        IPage<BmsDict> bmsDictList = null;
        try {
            bmsDictList = bmsDictService.findListByPage(bmsDictDTO, PageRequest.of(1, Integer.MAX_VALUE));
        } catch (BMSException e) {
            log.error("分页查询出错：{}", e.getMessage());
            throw new BMSException(CodeModelSystemEnums.ERROR_READING_FAST_MODEL);
        }

        return bmsDictList.getRecords();

    }

}
